clear
clc
warning off

work_path = 'C:\Users\Jiyuan\Documents\Work\';
proj_path = [work_path, '/CEFMC-code_release/'];
addpath(genpath(proj_path));

data_path = [work_path, 'datasets/mData/Fmatrix/'];
data_name = 'Mfeat'; % 'ORL', 'Flower17', 'Mfeat', 'BDGP', 'SUNRGBD', 'DryBean', 'AwA'

alg_name = 'CEFMC';
note = 'This can be filled with cmputer specifications.';

fprintf('\n# data_name: %s', data_name);
load([data_path, data_name, '_fea'], 'X', 'Y'); Y = double(Y);
k = length(unique(Y));
V = length(X);
n = size(X{1}, 2);

for v = 1:V
    X{v} = NormalizeData(X{v})';
end

% parameters
lambda_set = 2.^[-10:1:10];
c = k;
n_iters = 10;

metrics_meaning = {'acc'; 'nmi'; 'purity'; 'AR'; 'RI'; 'MI'; 'HI'; 'fscore'; 'precision'; 'recall'};

res = zeros(length(lambda_set), n_iters, length(metrics_meaning));
ts = zeros(length(lambda_set), n_iters);
times = cell(length(lambda_set), n_iters);
objs = cell(length(lambda_set), n_iters);
ys = cell(length(lambda_set), n_iters);

for i=1:length(lambda_set)
    lambda = lambda_set(i);
    beta = lambda;
    for iter=1:n_iters
        % main algorithm
        % tic;
        [y, ~, obj_tmp, times_tmp] = cefmc(X, lambda, beta, c, k);
        % tstmp = toc;
        tstmp = sum(times_tmp, 'all');
        [eval] = my_eval_y(y, Y);
        eval = eval*100;
        fprintf('\nlambda: %d, beta: %d, iter: %d, time: %.2f, acc: %.2f, nmi: %.2f, pur: %.2f', lambda, beta, length(obj_tmp{1}), tstmp, eval(1), eval(2), eval(3)); 
        res(i,iter,:) = eval;
        ts(i,iter) = tstmp; times{i,iter} = times_tmp;
        objs{i,iter} = obj_tmp;
        ys{i,iter} = y;
    end

    save([proj_path, 'res/', data_name, '_', alg_name, '_res.mat'], 'data_name', 'alg_name' , 'res', 'ts', 'times', 'ys', 'objs', 'lambda_set', 'note');
end
